package edu.yngsxy.demo.service.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.yngsxy.demo.mapper.TUserMapper;
import edu.yngsxy.demo.model.TUser;
import edu.yngsxy.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private TUserMapper tUserMapper;

    /**
     * 用户登录操作方法
     *
     * @param userName
     * @param password
     * @return 成功则返回VUser，失败返回null
     */
    @Override
    public TUser login(String userName, String password) {
        QueryWrapper<TUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userName",userName);
        queryWrapper.eq("password",password);
        TUser tUser = tUserMapper.selectOne(queryWrapper);
        return tUser;
    }

    /**
     * 检查某userName是否可用（已经存在就不可用）
     *
     * @param userName
     * @return 可用（数据库中没有）返回true， 否则返回false
     */
    @Override
    public boolean isUseridValid(String userName) {
        QueryWrapper<TUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userName",userName);
        TUser tUser = tUserMapper.selectOne(queryWrapper);
        if(tUser==null){
            return true;
        }
        return false;
    }

    /**
     * 注册用户（添加一个新用户）
     *
     * @return boolean  成功返回true, 失败返回false
     */
    @Override
    public boolean registerUser(String userName,String password) {
        TUser tUser = new TUser();
        tUser.setUsername(userName);
        tUser.setPassword(password);
        if(tUserMapper.insert(tUser)>0){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 修改一个用户信息
     *
     * @param user
     * @return boolean  成功返回true,失败返回false
     */
    @Override
    public boolean modifyUser(@Validated TUser user) {
        QueryWrapper<TUser> wrapper = new QueryWrapper<>();
        wrapper.eq("userid",user.getUserid());
        System.out.println(user+"----------------------------------------");
        if(tUserMapper.update(user,wrapper)>0){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 根据userid返回一个TUser类型的用户对象
     *
     * @param userName
     * @return TUser   失败返回null
     */
    @Override
    public TUser getTUserByid(String userName) {
        return null;
    }
}
